﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
//using System.Xml.Linq;
using System.Data;
using KPIS.Utils;
using KPIS.DBM;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.MODEL;
using Telerik.Web.UI;


namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class PR_IncomeType : System.Web.UI.UserControl
    {
        private object _dataItem = null;
        private SsMng ssMng = new SsMng();
        private IIncomeTypeBLL incomeTypeBLL = new IncomeTypeBLL();
        private IEmployeeTypeBLL empTypeBLL = new EmployeeTypeBLL();
        private IMasRecordStatusBLL recStaBLL = new MasRecordStatusBLL();
        private ICommandTypeBLL cmdTypeBLL = new CommandTypeBLL();

        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }
        UpperComboBLL mBLL = new UpperComboBLL();


        protected bool IsUserControlPostBack
        {
            get
            {
                return this.ViewState["IsUserControlPostBack"] != null;
            }
        }
        //Initializing the new variable updating ViewState data.
        protected override void OnLoad(System.EventArgs e)
        {
            base.OnLoad(e);
            txtExtAmount.Attributes.Add("onkeypress", "return isNumberKeyDotComma(event)");
            if (!IsUserControlPostBack)
            {
                // bindEmpType();
                bindStaRec();
                bindCommandType();
                this.ViewState.Add("IsUserControlPostBack", true);
            }

            if (DataItem != null)
            {
                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    lblID.Text = DataBinder.Eval(DataItem, "ext_seq").ToString();
                    // ddlEmployeeType.SelectedValue = DataBinder.Eval(DataItem, "emp_type_seq").ToString();
                    lblEmpType.Text = DataBinder.Eval(DataItem, "emp_type_seq").ToString();
                    txtCode.Text = DataBinder.Eval(DataItem, "ext_code").ToString();
                    txtName.Text = DataBinder.Eval(DataItem, "ext_name").ToString();
                    txtNameShort.Text = DataBinder.Eval(DataItem, "ext_name_short").ToString();
                    txtExtAmount.Text = DataBinder.Eval(DataItem, "ext_amount", "{0:N2}").ToString();
                    string tax = DataBinder.Eval(DataItem, "ext_tax").ToString();
                    if (tax == "Y")
                        rdoTax.Items[0].Selected = true;
                    else
                        rdoTax.Items[1].Selected = true;

                    chkMonthly.Checked = DataBinder.Eval(DataItem, "ext_monthly").ToString() == "Y";
                    ddlCommandType.SelectedValue = DataBinder.Eval(DataItem, "cmd_type_seq").ToString();
                    ddlStatusRec.SelectedValue = DataBinder.Eval(DataItem, "record_status").ToString();
                    txtRemarks.Text = DataBinder.Eval(DataItem, "remarks").ToString();
                }
                else
                {
                    lblEmpType.Text = Session["empTypeID"].ToString();
                }
            }
        }
        
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            try
            {
                double dAmount = 0;
                bool bAmount = double.TryParse(txtExtAmount.Text, out dAmount);
                Nullable<double> ndAmount = dAmount == 0 ? (double?)null : dAmount;
                string c = ddlCommandType.SelectedValue;
                string ExtMonthly = chkMonthly.Checked ? "Y" : "N";
                Nullable<int> nCmdType = c.Length > 0 ? Int32.Parse(c) : (int?)null;

                incomeTypeBLL.Insert(int.Parse(lblEmpType.Text), txtCode.Text, txtName.Text, txtNameShort.Text
                    , ndAmount, nCmdType, "I", rdoTax.SelectedValue, ExtMonthly
                    , txtRemarks.Text, ddlStatusRec.SelectedValue, ssMng.EmpSeq);
            }
            catch (Exception ex)
            {

            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                double dAmount = 0;
                bool bAmount = double.TryParse(txtExtAmount.Text, out dAmount);
                Nullable<double> ndAmount = dAmount == 0 ? (double?)null : dAmount;
                string c = ddlCommandType.SelectedValue;
                string ExtMonthly = chkMonthly.Checked ? "Y" : "N";
                Nullable<int> nCmdType = c.Length > 0 ? Int32.Parse(c) : (int?)null;

                incomeTypeBLL.Update(int.Parse(lblID.Text), int.Parse(lblEmpType.Text), txtCode.Text, txtName.Text
                    , txtNameShort.Text, ndAmount, nCmdType, "I", rdoTax.SelectedValue, ExtMonthly
                    , txtRemarks.Text, ddlStatusRec.SelectedValue, ssMng.EmpSeq);
            }
            catch (Exception ex)
            {

            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }

        private void bindStaRec()
        {
            IList<MasRecordStatusInfo> InfoList = recStaBLL.ListRecordStatus();
            ddlStatusRec.DataSource = InfoList;
            ddlStatusRec.DataBind();
        }
        private void bindCommandType()
        {
            DataSet InfoList = cmdTypeBLL.LoadCommandTypes((int?)null, 8);

            ddlCommandType.DataSource = InfoList;
            ddlCommandType.DataBind();
        }
    }
}